import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

/**
 * User: deepal
 * Date: Dec 4, 2010
 * Time: 5:05:31 PM
 */
public class MultiClient {
    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            System.exit(0);
        }
        String address = args[0];
        int numberOfThread = Integer.parseInt(args[2]);
        int port = Integer.parseInt(args[1]);
        MultiClient cl = new MultiClient();
        cl.start(address, port, numberOfThread);
    }

    public static boolean run = true;

    public void start(String address, int port, int numberOfThread) throws Exception {
        ArrayList<ResVale> value = new ArrayList<ResVale>();
  for (int i = 0; i < numberOfThread; i++) {
            Worker worker = new Worker(address, port, value);
            worker.start();
        }
        Thread.sleep(180000);
        run = false;
        double val = 0;
        for(int i=0; i<value.size(); i++){
            if(value.get(i) !=null) {
                val = val + value.get(i).value;
            }
        }
        System.out.println("Threads: "+numberOfThread +" RT:" + (val/value.size()) + " TP: "+ (value.size()/180));
        System.exit(0);
    }

    class Worker extends Thread {
        ServiceClient client;
        ArrayList value;

        Worker(String ip, int port, ArrayList value) throws Exception {
            client = new ServiceClient();
            Options opts = new Options();
            opts.setTo(new EndpointReference(
                    "http://" + ip + ":" + port + "/axis2/services/Version/getVersion"));
            client.setOptions(opts);
            opts.setProperty(Constants.Configuration.ENABLE_REST, Boolean.TRUE);
            opts.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
            this.value = value;
        }

        public void run() {
            long startTime;
            long endTime;
            while (run) {
                try {
                    startTime = System.currentTimeMillis();
                    client.sendReceive(createPayLoad());
                    endTime = System.currentTimeMillis();
                    value.add(new ResVale(endTime -startTime));
                    sleep(10);
                } catch (Exception axisFault) {
                    continue;
                }
            }
            //To change body of implemented methods use File | Settings | File Templates.
        }
    }

    public static OMElement createPayLoad() {

        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://ws.apache.org/axis2", "ns");
        OMElement method = fac.createOMElement("sayHello", omNs);
        OMElement value = fac.createOMElement("name", omNs);
        method.addChild(value);
        value.setText("Axis2");
        return method;
    }

    class ResVale{
       long value;

        ResVale(long value) {
            this.value = value;
        }
    }
}
